package com.srwl.mytx.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

final class MyResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private String TAG = "MyResponseBodyConverter";
    private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];

    private Type mType;
    private ParserConfig config;
    private int featureValues;
    private Feature[] features;

    MyResponseBodyConverter(Type type, ParserConfig config, int featureValues,
                            Feature... features) {
        mType = type;
        this.config = config;
        this.featureValues = featureValues;
        this.features = features;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String str_body = new String(value.bytes());
       // JSONObject jsonObject = JSONObject.parseObject(encrypt);

//        Log.d(TAG, "解密前内容：" + encrypt);
//        String s = "";
//        try {
//            byte[] decrypt = HttpEncryptUtil.appDecrypt(jsonObject);
//            s = new String(decrypt);
//        } catch (Exception e) {
//            e.printStackTrace();
//            Log.e(TAG, "解密出错：" + e.getContent());
//        }
//
//        Log.d(TAG, "解密后内容：" + s);
        try {
            return JSON.parseObject(str_body, mType, config, featureValues,
                    features != null ? features : EMPTY_SERIALIZER_FEATURES);
        } finally {
            value.close();
        }
    }
}
